﻿using BLL;
using ConvertMediaPro.Models;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ConvertMediaPro.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/
        private string WxUrl()
        {
            return System.Configuration.ConfigurationManager.AppSettings["wXUrl"];
        }
        public ActionResult Index()
        {
            Category_BLL bll = new Category_BLL();
            CategoryViewModel model = new CategoryViewModel();
            model.CategoryList= bll.SearchCategoryList();
            model.WxUrl = WxUrl();
            model.Name = "";
            return View(model);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(string name)
        {
            Category_BLL bll = new Category_BLL();
            Category model = new Category();
            model.Name = name;
            model.IsValid = true;
            model.CreateTime = DateTime.Now;
            model.LastUpdateTime = DateTime.Now;
            bll.Add(model);
            return RedirectToAction("Index");
        }

        public JsonResult ModifyInit(int id)
        {
            Category_BLL bll = new Category_BLL();
            Model.Category model = bll.GetById(id);
            return Json(new { cateName = model.Name,cateId=model.Id }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Modify(Int64 id, string name)
        {
            Category_BLL bll = new Category_BLL();
            bool result = bll.Update(name,id);
            return RedirectToAction("Index");
        }

        public ActionResult Delete(Int64 id)
        {
            Category_BLL bll = new Category_BLL();
            bool result = bll.Delete(id);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
	}
}